package com.tcs.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tcs.model.Article;
import com.tcs.model.Brand;
import com.tcs.service.ArticleService;

@Repository("brandDao")
public class BrandDaoImpl implements BrandDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Resource
	private ArticleDao articleDao;

	@Transactional
	public void saveBrand(Brand brand) {
		sessionFactory.getCurrentSession().saveOrUpdate(brand);
	}

	@Transactional
	public void updateBrand(Brand brand) {
		sessionFactory.getCurrentSession().update(brand);
	}

	@Transactional
	public void deleteBrand(int id) {
		articleDao.deleteListOfArticle(id);
		sessionFactory.getCurrentSession().delete(this.getBrand(id));
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Brand> listBrands() {
		return (List<Brand>) sessionFactory.getCurrentSession()
				.createCriteria(Brand.class).list();
	}

	@Transactional(readOnly = true)
	public Brand getBrand(int ID) {
		Brand brand = (Brand) sessionFactory.getCurrentSession()
				.createQuery("FROM Brand B WHERE B.brandId = " + ID + "")
				.uniqueResult();
		return brand;
	}

}
